using System.Collections.Generic;
using I2.Loc;
using Prime31;
using UnityEngine;

public class GotchaManager : MonoBehaviour
{
	private static GotchaManager _instance;

	public GotchaChest WoodenChest;

	public GotchaChest BronzeChest;

	public GotchaChest SilvernChest;

	public GotchaChest GoldenChest;

	public float costFrontier = 2000f;

	public float costMultyplyer = 1f;

	public List<GotchaChest> allCests = new List<GotchaChest>();

	private List<int> _notificationIds = new List<int>();

	public static GotchaManager instance
	{
		get
		{
			if (_instance == null)
			{
				Object.Instantiate(Resources.Load<GameObject>("GotchaManager")).name = "SessionReputationManager(Don't Destroy)";
			}
			return _instance;
		}
	}

	public bool gotLastSpinReward
	{
		get
		{
			return BCWStorage.Get<bool>("GotLastSpinReward");
		}
		set
		{
			BCWStorage.Set("GotLastSpinReward", value);
		}
	}

	public GotchaReward lastSpinReward
	{
		get
		{
			GotchaReward gotchaReward = new GotchaReward();
			gotchaReward.id = BCWStorage.Get<string>("GotLastSpinReward_ID");
			if (gotchaReward.id.Equals("cash"))
			{
				gotchaReward.type = GotchaRewardType.Cash;
			}
			else if (gotchaReward.id.Equals("gold"))
			{
				gotchaReward.type = GotchaRewardType.Gold;
			}
			else
			{
				gotchaReward.type = GotchaRewardType.Product;
			}
			gotchaReward.amount = BCWStorage.Get<int>("GotLastSpinReward_AMOUNT");
			return gotchaReward;
		}
		set
		{
			BCWStorage.Set("GotLastSpinReward_ID", value.id);
			BCWStorage.Set("GotLastSpinReward_AMOUNT", value.amount);
		}
	}

	private void Awake()
	{
		_instance = this;
		if (WoodenChest.count == 0)
		{
			WoodenChest.count = 1;
			WoodenChest.unlockTime = 1;
		}
		RemoveNotifications();
		allCests.Add(GoldenChest);
		allCests.Add(SilvernChest);
		allCests.Add(BronzeChest);
		allCests.Add(WoodenChest);
		foreach (GotchaChest allCest in allCests)
		{
			allCest.ResetTimer();
		}
	}

	public void AddChest(GotchaChestType chestType, int count = 1)
	{
		switch (chestType)
		{
		case GotchaChestType.Wooden:
			WoodenChest.Add(count);
			break;
		case GotchaChestType.Bronze:
			BronzeChest.Add(count);
			break;
		case GotchaChestType.Silvern:
			SilvernChest.Add(count);
			break;
		case GotchaChestType.Golden:
			GoldenChest.Add(count);
			break;
		}
		if (GotchaShowButton.instance != null)
		{
			GotchaShowButton.instance.SetUI();
		}
	}

	public GotchaReward GetSpinReward(List<GotchaReward> rewardList, GotchaChest chest)
	{
		float num = 0f;
		foreach (GotchaReward reward in rewardList)
		{
			num += reward.cost;
		}
		float num2 = 0f;
		foreach (GotchaReward reward2 in rewardList)
		{
			if (chest.moneyBox > reward2.cost * chest.costModifier)
			{
				reward2.weight = 10f;
			}
			else
			{
				reward2.weight = num / reward2.cost;
			}
			num2 += reward2.weight;
		}
		float num3 = Random.Range(0f, num2);
		float num4 = 0f;
		for (int i = 0; i < rewardList.Count; i++)
		{
			num4 += rewardList[i].weight;
			if (num3 < num4)
			{
				if (rewardList[i].cost >= costFrontier)
				{
					chest.moneyBox -= rewardList[i].cost * costMultyplyer;
				}
				else
				{
					chest.moneyBox += rewardList[i].cost * 0.15f;
				}
				return rewardList[i];
			}
		}
		return null;
	}

	private void OnApplicationPause(bool onPause)
	{
		if (!onPause)
		{
			RemoveNotifications();
			if ((bool)GotchaManagerUI.instance)
			{
				GotchaManagerUI.instance.HideGotchaUI();
			}
			if (GotchaShowButton.instance != null)
			{
				GotchaShowButton.instance.SetUI();
			}
		}
		else
		{
			if (!JSONManager.Instance.ServerTimeLoad())
			{
				return;
			}
			int num = 0;
			int num2 = 0;
			List<GotchaChest> list = new List<GotchaChest>(allCests);
			list.Sort((GotchaChest a, GotchaChest b) => a.unlockTime.CompareTo(b.unlockTime));
			foreach (GotchaChest item4 in list)
			{
				if (!item4.IsUnlocked() && item4.unlockTime - JSONManager.Instance.GetServerTime() > 0)
				{
					switch (num)
					{
					case 0:
					{
						int num4 = item4.unlockTime - JSONManager.Instance.GetServerTime();
						string title2 = string.Format(ScriptLocalization.Get("Game_Common/push_one_chest_title"), ScriptLocalization.Get("Game_Common/" + item4.type));
						string subtitle2 = ScriptLocalization.Get("Game_Common/push_one_chest_info");
						AndroidNotificationConfiguration androidNotificationConfiguration2 = new AndroidNotificationConfiguration(num4, title2, subtitle2, string.Empty);
						androidNotificationConfiguration2.largeIcon = "large_icon";
						androidNotificationConfiguration2.smallIcon = "small_icon";
						int item2 = EtceteraAndroid.scheduleNotification(androidNotificationConfiguration2);
						_notificationIds.Add(item2);
						break;
					}
					case 1:
					{
						int num3 = item4.unlockTime - JSONManager.Instance.GetServerTime();
						string title = ScriptLocalization.Get("Game_Common/push_many_chests_title");
						string subtitle = ScriptLocalization.Get("Game_Common/push_many_chests_info");
						AndroidNotificationConfiguration androidNotificationConfiguration = new AndroidNotificationConfiguration(num3, title, subtitle, string.Empty);
						androidNotificationConfiguration.largeIcon = "large_icon";
						androidNotificationConfiguration.smallIcon = "small_icon";
						int item = EtceteraAndroid.scheduleNotification(androidNotificationConfiguration);
						_notificationIds.Add(item);
						break;
					}
					}
					num++;
				}
				if (item4.IsUnlocked())
				{
					num2++;
				}
			}
			if (num2 > 0)
			{
				int num5 = 1800;
				string title3 = ScriptLocalization.Get("Game_Common/push_chest_was_opened_title");
				string subtitle3 = ScriptLocalization.Get("Game_Common/push_chest_was_opened_info");
				AndroidNotificationConfiguration androidNotificationConfiguration3 = new AndroidNotificationConfiguration(num5, title3, subtitle3, string.Empty);
				androidNotificationConfiguration3.largeIcon = "large_icon";
				androidNotificationConfiguration3.smallIcon = "small_icon";
				int item3 = EtceteraAndroid.scheduleNotification(androidNotificationConfiguration3);
				_notificationIds.Add(item3);
			}
		}
	}

	private void RemoveNotifications()
	{
		foreach (int notificationId in _notificationIds)
		{
			EtceteraAndroid.cancelNotification(notificationId);
		}
	}
}
